﻿using System.Windows;
using AuchanScan.ViewModels;
using Huyn.QRCode;
using Microsoft.Phone.Controls;
using System.Windows.Media;
using System;
using System.Windows.Controls;

namespace AuchanScan.Views
{
    public partial class EnCaissePage : PhoneApplicationPage
    {
        public EnCaissePage()
        {
            InitializeComponent();


            string data = ViewModelLocator.PayViewModelStatic.GenerateCodeForQRCode();
            var enc = new QRCodeGenerator();
            enc.EncodeMode = QRCodeGenerator.MODE.NUMERIC;
            QRCode.Source = enc.Generate(data);
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);

            if(ValidBasketGrid.Visibility == Visibility.Visible)
            {
                ValidBasketGrid.Visibility=Visibility.Collapsed;
                e.Cancel = true;
            }
        }

        private void ApplicationBarMenuItem_Click(object sender, System.EventArgs e)
        {
    ValidBasketGrid.Visibility=Visibility.Visible;
            ValidBasket.Focus();
        }
        bool passed;
        private void ValidBasket_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            if (ValidBasket.Text.Length == 4)
            {
                if (ValidBasket.Text == "1908")
                {
                    ((TextBox)sender).TextChanged -= ValidBasket_TextChanged;
                    if (!passed)
                    {
                        passed = true;
                        ViewModelLocator.MainViewModelStatic.ValidBasket();
                        NavigationService.Navigate(new Uri("/Views/ValidBasketPage.xaml", UriKind.Relative));
                    }
                }
                else
                    ValidBasket.Text = "";

            }
        }
    }
}